<?php
//亲测网 www.qincewang.com 独家解密
defined('IN_IA') or die('Access Denied');
class Cjwy_zhongqiuModuleSite extends WeModuleSite
{
	public function doWebFansinfo()
	{
		global $_GPC, $_W;
		$op = $_GPC['op'];
		if (empty($op)) {
			$op = 'display';
		}
		$uniacid = $_W['uniacid'];
		if ($op == 'display') {
			$uniacid = $_W['uniacid'];
			$pindex = max(1, intval($_GPC['page']));
			$psize = 20;
			$ptotal = 0;
			$plist = array();
			$list = pdo_fetchall("SELECT * FROM " . tablename('cjwyzq_player') . " WHERE uniacid = '{$uniacid}' ORDER BY `max_score` DESC LIMIT " . ($pindex - 1) * $psize . ",{$psize}");
			$total = pdo_fetchcolumn("SELECT COUNT(*) FROM " . tablename('cjwyzq_player') . " where uniacid='{$uniacid}'");
			$pager = pagination($total, $pindex, $psize);
		} else {
			if ($op == 'delete') {
				$id = $_GPC['id'];
				pdo_delete('cjwyzq_player', array('id' => $id));
				message('删除成功!', $this->createWebUrl('fansinfo', array('op' => 'display'), 'success'));
			} else {
				if ($op == 'update') {
					global $_GPC, $_W;
					$id = $_GPC['id'];
					$score = $_GPC['max_score'];
					$player = pdo_fetch("SELECT * FROM " . tablename('cjwyzq_player') . " WHERE id = '{$id}'");
					if (pdo_update('cjwyzq_player', array('max_score' => $score), array('id' => $id)) === false) {
						die('0');
					} else {
						die('1');
					}
				}
			}
		}
		include $this->template('fansinfo');
	}
	public function doWebRankset()
	{
		global $_GPC, $_W;
		load()->func('tpl');
		$rank = pdo_fetch("select * from " . tablename('cjwyzq_rank') . " where uniacid ='{$_W['uniacid']}'");
		if (checksubmit('submit')) {
			$rankdata = array('gid' => 1, 'uniacid' => $_W['uniacid'], 'ptitle' => $_GPC['ptitle'], 'pheadpic' => $_GPC['pheadpic'], 'psharetitle' => $_GPC['psharetitle'], 'psharedesc' => $_GPC['psharedesc'], 'psharepic' => $_GPC['psharepic']);
			if ($rank) {
				if (pdo_update('cjwyzq_rank', $rankdata, array('uniacid' => $_W['uniacid'])) === false) {
					message('排行榜数据更新失败！');
				} else {
					message('排行榜数据更新成功！', $this->createWebUrl('rankset', array('uniacid' => $uniacid)), 'success');
				}
			} else {
				if (pdo_insert('cjwyzq_rank', $rankdata) === false) {
					message('排行榜数据设置失败！');
				} else {
					message('排行榜数据设置成功！', $this->createWebUrl('rankset', array('uniacid' => $uniacid)), 'success');
				}
			}
		}
		include $this->template('rankset');
	}
	public function doWebRule()
	{
		global $_GPC, $_W;
		load()->func('tpl');
		$uniacid = $_W['uniacid'];
		$rule = pdo_fetch("select * from " . tablename('cjwyzq_rule') . " where uniacid ='{$_W['uniacid']}'");
		if (checksubmit('submit')) {
			$ruledata = array('gid' => 1, 'uniacid' => $_W['uniacid'], 'rtitle' => $_GPC['rtitle'], 'rheadpic' => $_GPC['rheadpic'], 'award' => htmlspecialchars_decode($_GPC['award']), 'rule' => htmlspecialchars_decode($_GPC['rule']), 'rsharetitle' => $_GPC['rsharetitle'], 'rsharedesc' => $_GPC['rsharedesc'], 'rsharepic' => $_GPC['rsharepic']);
			if ($rule) {
				if (pdo_update('cjwyzq_rule', $ruledata, array('uniacid' => $_W['uniacid'])) === false) {
					message('奖品数据更新失败！');
				} else {
					message('奖品数据更新成功！', $this->createWebUrl('rule', array('uniacid' => $uniacid)), 'success');
				}
			} else {
				if (pdo_insert('cjwyzq_rule', $ruledata) === false) {
					message('奖品数据设置失败！');
				} else {
					message('奖品数据设置成功！', $this->createWebUrl('rule', array('uniacid' => $uniacid)), 'success');
				}
			}
		}
		include $this->template('rule');
	}
	public function doWebMore()
	{
		global $_GPC, $_W;
		load()->func('tpl');
		$uniacid = $_W['uniacid'];
		$more = pdo_fetch("SELECT * FROM " . tablename('cjwyzq_more') . " where uniacid='{$_W['uniacid']}'");
		$places = array('province' => $more['province'], 'city' => $more['city'], 'district' => $more['area']);
		if (checksubmit()) {
			$moredata = array('uniacid' => $uniacid, 'status' => $_GPC['status'], 'url' => $_GPC['url'], 'starttime' => strtotime($_GPC['datelimit']['start']), 'endtime' => strtotime($_GPC['datelimit']['end']), 'province' => $_GPC['place']['province'], 'city' => $_GPC['place']['city'], 'area' => $_GPC['place']['district'], 'outtips' => $_GPC['outtips']);
			if ($more) {
				if (pdo_update('cjwyzq_more', $moredata) === false) {
					message('数据更新失败，请重新填写！', $this->createWebUrl('more'), 'error');
				} else {
					message('数据更新成功！', $this->createWebUrl('more'), 'success');
				}
			} else {
				if (pdo_insert('cjwyzq_more', $moredata) === false) {
					message('数据保存失败，请重新填写！', $this->createWebUrl('more'), 'error');
				} else {
					message('数据保存成功！', $this->createWebUrl('more'), 'success');
				}
			}
		}
		include $this->template('more');
	}
	public function doMobileIndex()
	{
		global $_GPC, $_W;
		$uniacid = $_W['uniacid'];
		$follow = $_W['fans']['follow'];
		$fansinfo = mc_oauth_userinfo();
		$more = pdo_fetch("SELECT * FROM " . tablename('cjwyzq_more') . " where uniacid='{$_W['uniacid']}'");
		$set = $this->module['config'];
		include $this->template('index');
	}
	public function doMobileRule()
	{
		global $_W;
		$listrule = pdo_fetch("SELECT * FROM " . tablename('cjwyzq_rule') . " WHERE uniacid='{$_W['uniacid']}'");
		include $this->template('rule');
	}
	public function doMobileRecord()
	{
		global $_GPC, $_W;
		$fetch_sql = 'SELECT * FROM ' . tablename('cjwyzq_player') . ' WHERE uniacid = :uniacid AND openid = :openid';
		$fetch_map = array(':uniacid' => intval($_GPC['uniacid']), ':openid' => $_GPC['openid']);
		if (!pdo_fetch($fetch_sql, $fetch_map)) {
			$player_data = array('uniacid' => intval($_GPC['uniacid']), 'openid' => $_GPC['openid'], 'nickname' => $_GPC['nickname'], 'avatar' => $_GPC['avatar'], 'max_score' => intval($_GPC['score']));
			$score_data = array('openid' => $_GPC['openid'], 'nickname' => $_GPC['nickname'], 'avatar' => $_GPC['avatar'], 'score' => intval($_GPC['score']), 'playtime' => $_GPC['playtime']);
			$player = pdo_insert('cjwyzq_player', $player_data);
			$score = pdo_insert('cjwyzq_score', $score_data);
			if ($player && $score) {
				$result['error'] = 0;
				$result['info'] = '添加得分记录成功';
				return json_encode($result);
			}
		} else {
			$score_data = array('openid' => $_GPC['openid'], 'nickname' => $_GPC['nickname'], 'avatar' => $_GPC['avatar'], 'score' => intval($_GPC['score']), 'playtime' => $_GPC['playtime']);
			if (pdo_insert('cjwyzq_score', $score_data)) {
				$max_sql = 'SELECT MAX(score) AS max_score FROM ' . tablename('cjwyzq_score') . ' WHERE openid = :openid';
				$max_map = array(':openid' => $_GPC['openid']);
				$max_score = pdo_fetch($max_sql, $max_map);
				$pscorelist = pdo_fetch("SELECT * FROM " . tablename('cjwyzq_player') . " WHERE openid = '{$_GPC['openid']}'");
				$pscore = $pscorelist['max_score'];
				$score = $max_score['max_score'];
				if ($pscore < $score) {
					$update_data = array('nickname' => $_GPC['nickname'], 'avatar' => $_GPC['avatar'], 'max_score' => intval($max_score['max_score']));
				} else {
					$update_data = array('nickname' => $_GPC['nickname'], 'avatar' => $_GPC['avatar'], 'max_score' => intval($pscore));
				}
				pdo_update('cjwyzq_player', $update_data, array('uniacid' => intval($_GPC['uniacid']), 'openid' => $_GPC['openid']));
				$result['error'] = 0;
				$result['info'] = '添加得分记录成功';
				return json_encode($result);
			}
		}
	}
}